//
//  TicTacModel.m
//  TicTacToe
//
//  Created by Tim on 09-11-23.
//  Copyright 2009 Tim Dimsdale. All rights reserved.
//
// This file is part of TicTacToe.
// 
// TicTacToe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// TicTacToe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with TicTacToe.  If not, see <http://www.gnu.org/licenses/>
//
#import "TicTacModel.h"


@implementation TicTacModel
// constructor
-(id) init {
	self = [super init];
	if ( self ) {
		[self clear];
		NSLog(@"TicTacModel created");
	}
	return self;
}// init

-(void)clear
{
	for ( int x=0; x<3; x++ ){
		for (int y=0; y<3; y++ ) {
			playingTable[x][y] = TICTAC_NOBODY;
		}
	}// for each horizontal
}

-(int) getPlayerAtX:(int)x andY:(int)y
{
	// should probably return an NSError.... 
	// \todo return error condition instead of potentially hiding bad code.
	// if you refactor this remember to put the bounds in isOtherPlayerAtX
	if ( x < 0 || y < 0 || x > 2 || y > 2 )
		return TICTAC_NOBODY;
	else
		return playingTable[x][y];
}

-(bool) isOtherPlayerAtX:(int)x andY:(int)y andIAm:(int)playerCode
{
	int playerAtPosition = [self getPlayerAtX:x andY:y];
	return ( playerAtPosition != playerCode && playerAtPosition != TICTAC_NOBODY);
}

-(void) setPlayerAtX:(int)x andY:(int)y withPlayer:(int)player error:(NSError**)error
{
//	[self debugDumpModel];
	//
	if ( [self isPositionAvailableAtX: x andY: y] ) 
	{
		//NSLog( @"Setting player at x = %d, y = %d, player = %d", (int)x, (int)y, (int)player);
		playingTable[(int)x][(int)y] = (int)player;
		[self debugDumpModel];
	} else {
		*error = [NSError errorWithDomain:@"TicTacModel" code:ERROR_PLAYING_SPACE_NOT_AVAILABLE userInfo:nil];	
	}
}// setPlayerAtX

-(bool) isPositionAvailableAtX:(int)x andY:(int)y
{
	return ( TICTAC_NOBODY == [self getPlayerAtX:x andY:y] );
}// isPositionAvailableAtX



-(int) getAnyWinner
{
	int winner = TICTAC_NOBODY;
	
	// check horizontal lines first
	for(int x=0; x<3; x++ ) 
		winner = ( playingTable[x][0] == playingTable[x][1] && playingTable[x][1] == playingTable[x][2] )?(playingTable[x][0]):winner;

	// check vertical lines after
	for(int y=0; y<3; y++ ) 
		winner = ( playingTable[0][y] == playingTable[1][y] && playingTable[1][y] == playingTable[2][y] )?(playingTable[0][y]):winner;
	
	// and then the diagonals
	winner = ( playingTable[0][0] == playingTable[1][1] &&  playingTable[2][2] == playingTable[1][1] ) ? (playingTable[1][1]):winner;
	winner = ( playingTable[2][0] == playingTable[1][1] &&  playingTable[0][2] == playingTable[1][1] ) ? (playingTable[1][1]):winner;
	
	return winner;
}

-(void) debugDumpModel
{
	NSLog(@"----------");
	for( int y=0; y<3; y++ ) {
		NSLog(@"[%d][%d][%d]", playingTable[0][y],playingTable[1][y],playingTable[2][y]);
	}
}// debugDumpModel

@end
